package com.control.qct.businesssteward.activity;

import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;

import com.control.qct.businesssteward.R;
import com.control.qct.businesssteward.base.BaseActivity;
import com.control.qct.businesssteward.fragment.BrandStatisticFragment;
import com.control.qct.businesssteward.fragment.ComplimentaryStatisticFragment;
import com.control.qct.businesssteward.fragment.DayStatisticTableFragment;
import com.control.qct.businesssteward.fragment.GoodsStatisticFragment;
import com.control.qct.businesssteward.fragment.MonthStatisticTableFragment;
import com.control.qct.businesssteward.fragment.SupplierStatisticFragment;
import com.control.qct.businesssteward.fragment.TypeStatisticFragment;
import com.control.qct.businesssteward.model.StoreModel;
import com.control.qct.businesssteward.utils.DateUtil;
import com.control.qct.businesssteward.utils.PopShopSelectUtils;
import com.control.qct.businesssteward.view.CustomCalendarView;

import butterknife.BindView;

/**
 * 销售报表模块界面
 * Created by Administrator on 2017/12/25 0025.
 */

public class StatisticalTableActivity extends BaseActivity implements CustomCalendarView.ICallbackClickListener,
        PopShopSelectUtils.ICallbackStoreListener {
    @BindView(R.id.ccv_date_time)
    CustomCalendarView mCcvDateTime;

    private int mId;
    private PopShopSelectUtils mPopShopSelectUtils;
    private TypeStatisticFragment mTypeStatisticFragment;
    private String mStoreId;
    private String mDate;
    private GoodsStatisticFragment mGoodsStatisticFragment;
    private MonthStatisticTableFragment mMonthStatisticTableFragment;
    private DayStatisticTableFragment mDayStatisticTableFragment;
    private BrandStatisticFragment mBrandStatisticFragment;
    private SupplierStatisticFragment mSupplierStatisticFragment;
    private ComplimentaryStatisticFragment mComplimentaryStatisticFragment;

    public StatisticalTableActivity() {
        super(R.layout.activity_statistical_table);
    }

    @Override
    protected void getBundleExtras(Bundle extras) {
        super.getBundleExtras(extras);
        mId = extras.getInt("id");
    }

    @Override
    protected void initView() {
        StoreModel storeModel = (StoreModel) mSPUtils.getObject(mNetProtocol.getUserCode() + "_store");
        mStoreId = storeModel == null ? "" : storeModel.getStoreId();
        mDate = DateUtil.toTime6(System.currentTimeMillis());
        //设置门店数据
        mPopShopSelectUtils = new PopShopSelectUtils();
        mPopShopSelectUtils.registerListener(this, mNetProtocol, this, 1);

        setLeftIcon(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                finish();
            }
        });
        setRightIcon("筛选", new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                mPopShopSelectUtils.setSingleBottomPop(StatisticalTableActivity.this, R.id.fl_content,
                        mStoreId);
            }
        });
        mCcvDateTime.setICallbackClickListener(this);
        replaceFragment();
    }

    /**
     * 选择显示哪个fragment
     */
    private void replaceFragment() {
        switch (mId) {
            case 1://日结统计表
                mCcvDateTime.setVisibility(true);
                mDayStatisticTableFragment = new DayStatisticTableFragment();
                replaceFragment(R.id.fl_content, mDayStatisticTableFragment);
                break;
            case 2://月结统计表
                mCcvDateTime.setVisibility(false);
                mMonthStatisticTableFragment = new MonthStatisticTableFragment();
                replaceFragment(R.id.fl_content, mMonthStatisticTableFragment);
                break;
            case 3://商品统计表
                mCcvDateTime.setVisibility(true);
                mGoodsStatisticFragment = new GoodsStatisticFragment();
                replaceFragment(R.id.fl_content, mGoodsStatisticFragment);
                break;
            case 4://品类统计表
                mCcvDateTime.setVisibility(true);
                mTypeStatisticFragment = new TypeStatisticFragment();
                replaceFragment(R.id.fl_content, mTypeStatisticFragment);
                break;
            case 5://品牌统计表
                mCcvDateTime.setVisibility(true);
                mBrandStatisticFragment = new BrandStatisticFragment();
                replaceFragment(R.id.fl_content, mBrandStatisticFragment);
                break;
            case 6://供应商统计表
                mCcvDateTime.setVisibility(true);
                mSupplierStatisticFragment = new SupplierStatisticFragment();
                replaceFragment(R.id.fl_content, mSupplierStatisticFragment);
                break;
            case 7://赠品统计表
                mCcvDateTime.setVisibility(true);
                mComplimentaryStatisticFragment = new ComplimentaryStatisticFragment();
                replaceFragment(R.id.fl_content, mComplimentaryStatisticFragment);
                break;
        }
    }

    @Override
    public void leftClick(String time) {
        mDate = time;
        if (mId == 2)
            mMonthStatisticTableFragment.getMonthStatistic(mDate, mStoreId);
    }

    @Override
    public void rightClick(String time) {
        mDate = time;
        if (mId == 2)
            mMonthStatisticTableFragment.getMonthStatistic(mDate, mStoreId);
    }

    @Override
    public void itemClick(String time) {
        mDate = time;
        setData();
    }

    @Override
    public void onSure(String storeId, String storeName) {
        mStoreId = storeId;
        if (mId == 2)
            mMonthStatisticTableFragment.getMonthStatistic(mDate, mStoreId);
        setData();
    }

    /**
     * 设置数据
     */
    private void setData() {
        switch (mId) {
            case 1://日结统计表
                mDayStatisticTableFragment.getDayStatistic(mDate, mStoreId);
                break;
            case 2://月结统计表
//                mMonthStatisticTableFragment.getMonthStatistic(mDate, mStoreId);
                break;
            case 3://商品统计表
                mGoodsStatisticFragment.getProductStatistic(mDate, mStoreId);
                break;
            case 4://品类统计表
                mTypeStatisticFragment.getTypeStatistic(mDate, mStoreId);
                break;
            case 5://品牌统计表
                mBrandStatisticFragment.getBrandStatistic(mDate, mStoreId);
                break;
            case 6://供应商统计表
                break;
            case 7://赠品统计表
                break;
        }
    }
}
